﻿package mortal.game.view.msgbroad
{
    import com.gengine.utils.*;
    import com.greensock.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.font.*;
    import mortal.component.gconst.*;

    public class NoticeItem extends Sprite
    {
        private const WIDTH:int = 270;
        private var _bmpDict:Array;
        private var _lineDict:Array;
        private var _widthDict:Array;
        private var _textField:TextField;
        private var _matix:Matrix;
        private var _height:int;
        private var _line:int;
        private var _timerKey:int;
        private var _outTween:TweenMax;
        private var _endFun:Function;
        private var _hideCallBack:Function;
        private var _hideIng:Boolean;
        public var disposed:Boolean;

        public function NoticeItem()
        {
            this._bmpDict = [];
            this._lineDict = [];
            this._widthDict = [];
            this._matix = new Matrix();
            mouseEnabled = false;
            mouseChildren = false;
            cacheAsBitmap = true;
            this._textField = new TextField();
            this._textField.selectable = false;
            this._textField.multiline = true;
            this._textField.wordWrap = true;
            this._textField.width = this.WIDTH;
            var _loc_1:* = new GTextFormat(FontUtil.songtiName, 14, 16776960, false, null, null, null, null, TextFormatAlign.LEFT, null, null, null, 4);
            _loc_1.letterSpacing = 2;
            this._textField.defaultTextFormat = _loc_1;
            this._textField.autoSize = TextFieldAutoSize.LEFT;
            return;
        }// end function

        private function onInEnd() : void
        {
            this._timerKey = setTimeout(this.timerOutHandler, 3000);
            return;
        }// end function

        public function onOutEnd() : void
        {
            if (this._endFun != null)
            {
                this._endFun.call(this, this);
            }
            return;
        }// end function

        private function onHideEnd() : void
        {
            this._hideIng = false;
            if (this._hideCallBack != null)
            {
                this._hideCallBack.call(this, this);
            }
            return;
        }// end function

        private function timerOutHandler() : void
        {
            var _loc_1:int = 0;
            var _loc_2:Bitmap = null;
            while (_loc_1 < numChildren)
            {
                
                _loc_2 = this._bmpDict[_loc_1];
                if (_loc_1 == 0)
                {
                    this._outTween = TweenMax.to(_loc_2, 0.2, {alpha:0.2, onComplete:this.onOutEnd});
                }
                else
                {
                    this._outTween = TweenMax.to(_loc_2, 0.2, {alpha:0.2});
                }
                _loc_1++;
            }
            return;
        }// end function

        public function updateData(param1:String, param2:Function, param3:int = 70, param4:Boolean = true) : void
        {
            var _loc_5:int = 0;
            var _loc_7:Bitmap = null;
            this.disposed = false;
            this._line = 0;
            this._height = 0;
            this._matix.ty = 0;
            this._lineDict.splice(0);
            this._widthDict.splice(0);
            this._textField.htmlText = HTMLUtil.addColor(param1, "#ffff00");
            this._textField.height = this._textField.textHeight;
            var _loc_6:* = this._textField.numLines;
            while (_loc_5 < _loc_6)
            {
                
                _loc_7 = this.getBmpByLine(_loc_5);
                addChild(_loc_7);
                _loc_5++;
            }
            this.resetPosx();
            this._endFun = param2;
            this.onInEnd();
            return;
        }// end function

        private function getBmpByLine(param1:int) : Bitmap
        {
            var _loc_4:TextLineMetrics = null;
            var _loc_2:* = this.getLineMetrics(param1);
            var _loc_3:* = this._bmpDict[param1];
            if (!_loc_3)
            {
                var _loc_5:* = new Bitmap();
                this._bmpDict[param1] = new Bitmap();
                _loc_3 = _loc_5;
                _loc_3.filters = [FilterConst.noticeItemFilter2, FilterConst.noticeItemFilter];
            }
            _loc_3.alpha = 1;
            _loc_3.bitmapData = this.getBmpdByLine(param1, _loc_2);
            if (param1 % 3 == 0)
            {
                var _loc_5:String = this;
                var _loc_6:* = this._line + 1;
                _loc_5._line = _loc_6;
            }
            if (param1 != 0)
            {
                _loc_4 = this.getLineMetrics((param1 - 1));
            }
            _loc_3.x = this.getLineSumWidth(this._line);
            if (_loc_4)
            {
                if (this._line > 1)
                {
                    _loc_3.y = (this._line - 1) * _loc_4.height;
                }
                else
                {
                    _loc_3.y = 0;
                }
            }
            else
            {
                _loc_3.y = 0;
            }
            if (!this._lineDict[(this._line - 1)])
            {
                this._lineDict[(this._line - 1)] = [];
            }
            this._lineDict[(this._line - 1)].push(_loc_3);
            if (!this._widthDict[(this._line - 1)])
            {
                this._widthDict[(this._line - 1)] = 0;
            }
            this._widthDict[(this._line - 1)] = this._widthDict[(this._line - 1)] + _loc_2.width;
            return _loc_3;
        }// end function

        private function getBmpdByLine(param1:int, param2:TextLineMetrics) : BitmapData
        {
            var _loc_3:* = param2.height;
            var _loc_4:* = param2.width;
            this._matix.ty = (-param1) * _loc_3;
            var _loc_5:* = new BitmapData(_loc_4, _loc_3, true, 0);
            _loc_5.draw(this._textField, this._matix);
            return _loc_5;
        }// end function

        private function getLineMetrics(param1:int) : TextLineMetrics
        {
            var tme:TextLineMetrics;
            var index:* = param1;
            try
            {
                tme = this._textField.getLineMetrics(index);
            }
            catch (e:Error)
            {
                tme = new TextLineMetrics(0, 20, 20, 0, 0, 0);
            }
            return tme;
        }// end function

        private function resetPosx() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:Array = null;
            var _loc_6:Bitmap = null;
            while (_loc_2 < this._line)
            {
                
                _loc_1 = (this.width - this._widthDict[_loc_2]) / 2;
                _loc_5 = this._lineDict[_loc_2];
                _loc_4 = _loc_5.length;
                _loc_3 = 0;
                while (_loc_3 < _loc_4)
                {
                    
                    _loc_6 = _loc_5[_loc_3];
                    _loc_6.x = _loc_6.x + _loc_1;
                    _loc_3++;
                }
                _loc_2++;
            }
            return;
        }// end function

        private function getLineSumWidth(param1:int) : int
        {
            if (this._widthDict.length >= param1)
            {
                return this._widthDict[(param1 - 1)];
            }
            return 0;
        }// end function

        public function easeHide(param1:Function) : void
        {
            this._hideIng = true;
            this._hideCallBack = param1;
            this.onHideEnd();
            return;
        }// end function

        public function killInEase() : void
        {
            this.onInEnd();
            return;
        }// end function

        public function dispose() : void
        {
            var _loc_1:Bitmap = null;
            this._textField.text = "";
            this._hideCallBack = null;
            this._hideIng = false;
            this._endFun = null;
            clearTimeout(this._timerKey);
            if (this._outTween && this._outTween.active)
            {
                this._outTween.kill();
            }
            while (numChildren > 0)
            {
                
                _loc_1 = removeChildAt(0) as Bitmap;
                _loc_1.bitmapData.dispose();
                _loc_1.bitmapData = null;
            }
            this.disposed = true;
            return;
        }// end function

        public function updatePosY(param1:int) : void
        {
            this.y = param1;
            return;
        }// end function

        public function get hideIng() : Boolean
        {
            return this._hideIng;
        }// end function

        override public function get height() : Number
        {
            if (this._height == 0)
            {
                this._height = this._line * 20;
            }
            return this._height;
        }// end function

        override public function get width() : Number
        {
            return 810;
        }// end function

    }
}
